let file = {
    getMsgName(args) {
        return `file_${args.callee.name}_${this.randomNum()}`
    },
    randomNum(len = 12) {
        return Math.floor(Math.pow(10, len) * Math.random())
    },
    readFile(param) {
        let msgName = this.getMsgName(arguments);
        let onData = (obj) => {
            if (param.onData) {
                param.onData(obj);
            }
        }
        let onFinish = (obj) => {
            if (param.onFinish) {
                param.onFinish(obj);
            }
            eventer.off(`${msgName}_data`, onData)
        }
        eventer.on(`${msgName}_data`, onData);
        eventer.once(`${msgName}_finish`, onFinish);
        window.nativeCall(msgName, JSON.stringify(param));
    }
}

let readFileBtn = document.querySelector("#readFileBtn");
readFileBtn.addEventListener("click", async () => {
    let result = "";
    let param = {
        filePath: "D:\\Example\\14.md",
        onData(chunk) {
            let decoder = new TextDecoder('utf-8', { ignoreBOM: true });
            console.log(chunk);
            let str = decoder.decode(chunk);
            result += str;
        },
        onFinish(data) {
            console.log("文件读取完成")
            console.log(result);
        }
    }
    file.readFile(param);
})